package com.uicole.push.server.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.uicole.push.server.model.AppInfo;
import com.uicole.push.server.service.impl.AppInfoServiceImpl;

@Controller
public class AppInfoController {

	@Autowired
	private AppInfoServiceImpl appInfoService;
	// AppInfos
	@RequestMapping(value = "/appInfos", method = RequestMethod.GET)
	public String appInfosList(Model model) {
		model.addAttribute("appInfos", appInfoService.getAll());
		return "appInfo/appInfos_index";
	}

	@RequestMapping(value = "/appInfos/new", method = RequestMethod.GET)
	public String appInfosNew(Model model) {
		model.addAttribute("appInfo", new AppInfo());
		return "appInfo/appInfos_create";
	}

	@RequestMapping(value = "/appInfos", method = RequestMethod.POST)
	public String appInfosCreate(Model model, AppInfo appInfo) {
		AppInfo existing = appInfoService.get(appInfo.getId());
		if (existing != null) {
			model.addAttribute("error", "AppInfo already exist !");
			model.addAttribute("appInfos", appInfoService.getAll());
			return "appInfo/appInfos_index";
		}
		appInfoService.save(appInfo);
		model.addAttribute("info", "AppInfo created successfully");
		return "appInfo/appInfos_get";
	}

	@RequestMapping(value = "/appInfos/{id}", method = RequestMethod.GET)
	public String appInfosRetrieve(@PathVariable Long id, Model model) {
		AppInfo appInfo = appInfoService.get(id);
		model.addAttribute("appInfo", appInfo);
		return "appInfo/appInfos_get";
	}

	@RequestMapping(value = "/appInfos/edit/{id}", method = RequestMethod.GET)
	public String appInfosEdit(@PathVariable Long id, Model model) {
		AppInfo appInfo = appInfoService.get(id);
		model.addAttribute("appInfo", appInfo);
		return "appInfo/appInfos_update";
	}

	@RequestMapping(value = "/appInfos/update", method = RequestMethod.POST)
	public String appInfosUpdate(Model model, AppInfo appInfo) {
		AppInfo oldAppInfo = appInfoService.get(appInfo.getId());
		
		model.addAttribute("info", "AppInfo updated successfully");
		model.addAttribute("appInfo", oldAppInfo);
		return "appInfo/appInfos_get";
	}

	@RequestMapping(value = "/appInfos/delete/{id}", method = { RequestMethod.GET })
	public String appInfosDelete(@PathVariable Long id, Model model) {
		appInfoService.remove(id);
		model.addAttribute("info", "AppInfo deleted successfully");
		model.addAttribute("appInfos", appInfoService.getAll());
		return "appInfo/appInfos_index";
	}

	@RequestMapping(value = "/appInfos/search", method = RequestMethod.POST)
	public String appInfosSearch(Model model, AppInfo appInfo) {
		List<AppInfo> appInfos = appInfoService.getAll();
		model.addAttribute("appInfos", appInfos);
		model.addAttribute("search", true);
		return "appInfo/appInfos_index";
	}

	public AppInfoServiceImpl getAppInfoService() {
		return appInfoService;
	}

	public void setAppInfoService(AppInfoServiceImpl appInfoService) {
		this.appInfoService = appInfoService;
	}

}
